#!/usr/bin/env python
#-*- coding:utf-8 -*-
#页面
from flask import Flask,render_template,url_for,redirect,request
import pacong
from char import openfile_read,openfile_write
from jpg import jpg_char

app=Flask(__name__)
#登陆
@app.route('/login/',methods = ['GET','POST'])
def login():
	if request.method=='POST':
		if openfile_read(request.form['username'],request.form['password']):
			i=request.form['username']
			return render_template('index.html',i=i)
		else:
			error="username or password error"
			return render_template('login.html',error=error)
	return render_template('login.html')
#注册
@app.route('/register/',methods = ['GET','POST'])
def register():
	if request.method=='POST':
		username=request.form['username']
		password=request.form['password']
		if openfile_write(username,password)==0:
			error='Registration failed. Please try again.'
			return render_template('register.html',error=error)
		else:
			return redirect(url_for('login'))
	return render_template('register.html')
#主页
@app.route('/')
def index():
	jpg=jpg_char()
	return render_template('index.html',jpg=jpg)
@app.route('/url_2')
def url_2():
	jpg=jpg_char()
	return render_template('url_2.html',jpg=jpg)
@app.route('/yule/')
#娱乐
def yule():
	tmp=pacong.yule('http://ent.sina.com.cn/')
	return render_template('yule.html',tmp=tmp)
@app.route('/tiyu/')
#体育
def tiyu():
	tmp=pacong.tiyu('http://sports.sina.com.cn/')
	return render_template('tiyu.html',tmp=tmp)
@app.route('/shehui/')
#社会
def shehui():
	tmp=pacong.shehui('http://news.sina.com.cn/society/')
	
	return render_template('shehui.html',tmp=tmp)
@app.route('/junshi/')
#军事
def junshi():
	tmp=pacong.junshi('http://mil.news.sina.com.cn/')
	return render_template('junshi.html',tmp=tmp)
if __name__ == '__main__':
	app.run()